/**
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import model.Model;

/**
 * @author Troy Ferrell
 *
 */
public class PropertiesFrame extends PicassaFrame implements Observer
{
	private static final int PADDING = 25;
	
	// Properties members
	private JList myVariableList;
	private JList myExpressionList;
	
	private JScrollPane myVariablePane;
	private JScrollPane myExpressionPane;
	
	public PropertiesFrame(Model newModel)
	{
		setModel(newModel);
		this.getContentPane().add(createProperties());
	}
	
	
	/**
	 * Create properties components for variables and expression lists
	 * 
	 * @return Container for property components
	 */
	public JPanel createProperties()
	{
		JPanel propertiesPanel = new JPanel();
		//propertiesPanel.setLayout(new BoxLayout(propertiesPanel, BoxLayout.Y_AXIS));
		
		CompoundBorder cb = new CompoundBorder(BorderFactory.createTitledBorder( 
				BorderFactory.createLoweredBevelBorder(), "Properties"), new EmptyBorder(10,10,10,10));
		propertiesPanel.setBorder(cb);
		
        myVariableList = createJList();
		myVariablePane = new JScrollPane(myVariableList);
		myVariablePane.setBorder( createListBorder("Variables" ));
		
		myExpressionList = createJList();
		myExpressionPane = new JScrollPane(myExpressionList);
		myExpressionPane.setBorder( createListBorder("Expressions") );
		
		propertiesPanel.add(myVariablePane, BorderLayout.NORTH);
		propertiesPanel.add(myExpressionPane, BorderLayout.SOUTH);
		
		return propertiesPanel;
	}
	
	/**
	 * Create a JList component 
	 * 
	 * @return the JList
	 */
	private JList createJList() 
	{
		JList returnList = new JList();// put data in here
		returnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		returnList.setLayoutOrientation(JList.VERTICAL);
		returnList.setVisibleRowCount(-1); // show max possible
		
		return returnList;
	}
	
	/**
	 * Create Border for scrollpanes that hold JLists of variables & expressions
	 * 
	 * @param the title for the border
	 * @return a titled border for lists
	 */
	private Border createListBorder(String title) 
	{
		TitledBorder expTitleBorder = BorderFactory.createTitledBorder( 
				BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), title);
		expTitleBorder.setTitleJustification(TitledBorder.CENTER);
		return expTitleBorder;
	}
	
	/**
	 *  Observer Interface(non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	public void update(Observable obs, Object obj)
	{
		// TODO: There is an error in the list components of the GUI
		// 	     Unfortunatley, variables & expressions seem to be aggregrating in the same list
		//       instead of into their separate lists
		// TODO: Could make this code more readable and possibly less duplicated, need to though first figure out
		//  	why Jlists are working properly before refactoring
		
		DefaultListModel varListModel = new DefaultListModel();
		
		for(String key : myModel.myVariables.keySet())
			varListModel.addElement( key + " - " + myModel.myVariables.get(key) );
		
		myVariableList.setModel(varListModel);
		
		
		DefaultListModel expListModel = new DefaultListModel();
		
		for(int i = 0; i < myModel.myExpressions.size(); i++)
			varListModel.addElement( "$" + i + " - " +  myModel.myExpressions.get(i) );
		
		myExpressionList.setModel(expListModel);
	
		this.repaint();
	}

	@Override
	public void setSize(Dimension d)
	{
		this.setSize(d.width, d.height);
	}
	
	@Override
	public void setSize(int width, int height)
	{
		super.setSize(width, height);
		myVariablePane.setPreferredSize(new Dimension(width - PADDING, height/2 - 2*PADDING));
		myExpressionPane.setPreferredSize(new Dimension(width - PADDING, height/2 - 2*PADDING));
	}
	
}
